/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package escom.ipn.administrador;

import escom.ipn.modelo.delegate.adminCRPDelegate;
import escom.ipn.modelo.delegate.adminPoliciaCRPDelegate;
import escom.ipn.modelo.dto.CRPClass;
import escom.ipn.modelo.dto.PoliciaClass;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Carlos
 */
@WebServlet(name = "actualizaPoliciaCRP", urlPatterns = {"/actualizaPoliciaCRP"})
public class actualizaPoliciaCRP extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
        adminCRPDelegate delCr;
        adminPoliciaCRPDelegate delC;
        adminPoliciaCRPDelegate delC1;
        CRPClass c ;
        PoliciaClass a ;
        adminCRPDelegate delCrUpdate;
        String nom, pat,mat, tel,dir, grado;
        int jor,id,asig;
        
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
                delCr = new adminCRPDelegate();
                delC = new adminPoliciaCRPDelegate();
                delC1 = new adminPoliciaCRPDelegate();
                a = new PoliciaClass();
                c = new CRPClass();
                delCrUpdate = new adminCRPDelegate();
                List listaCRP;
                List lista;
                List poli;
                
                a.setId_policia_pie_tierra(Integer.parseInt(request.getParameter("ID")));
                
                //asignar informacion acerca de la patrulla
                String nomcrp=request.getParameter("CRP");
                c.setNombre(nomcrp);
                listaCRP=delCr.getCRPID(c);
                c= (CRPClass)listaCRP.get(0);
                id=c.getId_carro_radio_patrulla();
                
                //Actualiza CRP a utilizar
                poli=delC.loadSelect(a);
                a= (PoliciaClass) poli.get(0);
                c.setId_carro_radio_patrulla(a.getCrp());
                lista=delCrUpdate.loadSelect(c);
                c=(CRPClass) lista.get(0);
                asig=c.getAsignado();
                asig-=1;
                c.setAsignado(asig);
                delCrUpdate = new adminCRPDelegate();
                delCrUpdate.asignacion(c);
                //Asignacion del Nuevo CRP
                delCrUpdate = new adminCRPDelegate();
                c.setId_carro_radio_patrulla(id);
                lista=delCrUpdate.loadSelect(c);
                c=(CRPClass) lista.get(0);
                asig=c.getAsignado();
                asig+=1;
                c.setAsignado(asig);
                delCrUpdate = new adminCRPDelegate();
                delCrUpdate.asignacion(c);
                
                //Asignacion de la información
                
                a.setCrp(id);
                nom=request.getParameter("nombre");
                pat=request.getParameter("ap_paterno");
                mat=request.getParameter("ap_materno");
                a.setNombre(nom);
                a.setAp_paterno(pat);
                a.setAp_materno(mat);
                dir=request.getParameter("direccion");
                tel=request.getParameter("tel");
                grado=request.getParameter("grado");
                a.setDireccion(dir);
                a.setTelefono(tel);
                a.setGrado(grado);
                jor=Integer.parseInt(request.getParameter("jornada"));
                a.setJornada(jor);
                
                if(a!=null){
                    delC1.updatePolicia(a);
                }
                } catch (SQLException ex) {
                Logger.getLogger(actualizaPolicia.class.getName()).log(Level.SEVERE, null, ex);
                } 
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
